<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="../plugins/select2/select2.css" />
    <link rel="stylesheet" href="../plugins/select2/select2-bootstrap.css" />
    <script src="../plugins/select2/select2.min.js" type="text/javascript"></script>
	<script src="../js/pages/public.js"></script>
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#addtModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="del()"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">

                                <div class="has-feedback">
									<form id="selectForm2">
							                  <input name="name" class="form-control" placeholder="规格名称">
										<input type="button" value="搜索" onclick="load()">
									</form>
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tbody">
			                         <!-- <tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>1</td>
									      <td>手机</td>
									      <td>联想,华为,中兴</td>
									      <td>网络制式</td>									 
									      <td></td>		                                                                  
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>-->
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->
							<!--数据列表/-->
							<a href="javascript:getpage(1)">首页</a>
							<a href="javascript:getpage(2)">上一页</a>
							<a>当前第<input name="pageNum" size="1" id="pageNum" value="1">页
								<a>每页显示<input id="pageSize" size="1">条记录共<input size="1" id="allCount">条记录</a>
								<a href="javascript:getpage(3)">下一页</a>
								<a href="javascript:getpage(4)">末页</a>
							</a>
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->

<!-- 增加窗口 -->
<div class="modal fade" id="addtModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myaddModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" name="name" id="name"  placeholder="商品名称"></td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
						<select id="addBrandList" name="brandIds" multiple style="width: 100%"></select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="addSpecList" name="specIds" multiple style="width: 100%"></select>
		      		</td>
		      	</tr>

				<tr>
					<td>扩展属性</td>
					<td>
						<div class="btn-group">
							<button type="button" class="btn btn-default" title="新增扩展属性" id="insertTr"><i class="fa fa-file-o"></i> 新增扩展属性</button>
						</div>
						<table class="table table-bordered table-striped"  width="800px" id="table">
							<thead>
							<tr>
								<!--									  <td><input type="checkbox" class="icheckbox_square-blue"></td>-->
								<td>属性名称</td>
								<td>操作</td>
							</tr>
							</thead>
							<tbody id="tbody1">
							<tr>
								<!--									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>-->
								<td><input class="form-control" placeholder="属性名称" name="customAttributeItems"></td>
								<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
							</tr>
							<tr>
								<!--									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>-->
								<td><input class="form-control" placeholder="属性名称" name="customAttributeItems"></td>
								<td><button type="button" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
							</tr>
							</tbody>
						</table>

					</td>
				</tr>
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="save()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
<!-- 修改窗口 -->
<div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
			<h3 id="myupdateModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
					<input name="id" id="idd" type="text">
		      		<td>商品类型</td>
		      		<td><input  class="form-control" id="updateName" name="name" placeholder="商品类型">  </td>
		      	</tr>
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
						<select id="updateBrandList" name="brandIds" multiple style="width: 100%"></select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="updateSpecList" name="specIds" multiple style="width: 100%"></select>
		      		</td>
		      	</tr>

		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                        </div>
						<table class="table table-bordered table-striped"  width="800px" id="table1">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>
									<td>操作</td>
								</tr>
							</thead>
							<tbody id="tbody2">

							</tbody>
						</table>
		      		</td>
		      	</tr>
			 </table>
		</div>

		<div class="modal-footer">
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateTemplate()">修改</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
<script>
	var token = window.localStorage.getItem("token");

	$(function () {
		selectBrnd();
		selectSpec();
		getpage(1)
	})

	function load() {
		select();
	}

	function select() {
		//获取条件查询的值
		var parms = $("#selectForm2").serializeObject();
		var page = {"pageNum":$("#pageNum").val(),"pageSize":5,"parms":parms};
		$.ajax({
			headers : {"AutharizeToken":token},
			url: domain+"/TypeTemplateController/typrselect",
			contentType: 'application/json;charset=utf-8',
			type:"post",
			dataType:"json",
			//将传到后台的数据转为json格式
			data: JSON.stringify(page),
			//异步请求
			async:false,
			success:function (result) {
				var s = ""
				var brand = "";
				var spec = "";
				var customAttributeItems = "";
				$(result.data.rows).each(function (i, v) {
					var brandList = JSON.parse(v.brandIds);
					var specList = JSON.parse(v.specIds);
					var customAttributeItemsList = JSON.parse(v.customAttributeItems);
					//品牌
					var ids = []
					$(brandList).each(function (i1,v1) {
						ids.push(v1.text)
					})
					brand = ids
					//规格
					var ids1 = []
					$(specList).each(function (i2, v2) {
						ids1.push(v2.text)
					})
					spec = ids1
					//扩展属性
					var ids2 = []
					$(customAttributeItemsList).each(function (i3, v3) {
						ids2.push(v3.text)
					})
					customAttributeItems = ids2
					s += "<tr><td><input  type='checkbox' value='" + v.id + "' name='check'></td>"
					s += "<td>" + v.id + "</td>"
					s += "<td>" + v.name + "</td>"
					s += "<td>" + brand + "</td>"
					s += "<td>" + spec + "</td>"
					s += "<td>" + customAttributeItems + "</td>"
					s += "<td class='text-center'>"
					s += "<button type='button' class='btn bg-olive btn-xs' data-toggle='modal' data-target='#updateModal' onclick='getTemplateById(" + v.id + ")'>修改</button>"
					s += "</td></tr>"
				})
					$("#tbody").html(s)
					$("#pageSize").val(result.data.pageSize);
					$("#pageNum").val(result.data.pageNum);
					$("#allCount").val(result.data.total);
			},error:function () {
				alert("系统异常")
			}
		})
	}

	function selectBrnd() {
		//获取条件查询的值
		var parms = {};
		var page = {"pageNum":$("#pageNum").val(),"pageSize":5,"parms":parms};
		$.ajax({
			headers : {"AutharizeToken":token},
			url: domain+"/brand/findBrandPage",
			contentType: 'application/json;charset=utf-8',
			type:"post",
			dataType:"json",
			//将传到后台的数据转为json格式
			data: JSON.stringify(page),
			//异步请求
			async:false,
			success:function (result) {
				if (result.code==10000){
					var list = result.data.rows;
					//拼接路径
					var  html = '';
					$(list).each(function(i,e) {
						html += '<option value="'+e.id+'">'+e.name+'</option>'
					})
					$("#addBrandList").html(html)
					$("#updateBrandList").html(html)
					$("#addBrandList").select2()
					$("#updateBrandList").select2()
				}
			},error:function () {
				alert("系统异常")
			}
		})
	}
	function selectSpec() {
		//获取条件查询的值
		var parms = {};
		var page = {"pageNum":$("#pageNum").val(),"pageSize":5,"parms":parms};
		$.ajax({
			headers : {"AutharizeToken":token},
			url: domain+"/spec/findSpecPage",
			contentType: 'application/json;charset=utf-8',
			type:"post",
			dataType:"json",
			//将传到后台的数据转为json格式
			data: JSON.stringify(page),
			//异步请求
			async:false,
			success:function (result) {
				if (result.code==10000){
					var list = result.data.rows;
					//拼接路径
					var  html = '';
					$(list).each(function(i,e) {
						html += '<option value="'+e.id+'">'+e.specName+'</option>'
					})
					$("#addSpecList").html(html)
					$("#updateSpecList").html(html)
					$("#addSpecList").select2()
					$("#updateSpecList").select2()
				}
			},error:function () {
				alert("系统异常")
			}
		})
	}

	function getpage(id) {
		var dang = parseInt($("#pageNum").val());
		var gong = parseInt($("#allCount").val());
		var zong = parseInt($("#pageSize").val());
		var o =gong-(gong%zong==0? gong/zong : gong%zong);
		var mo = (o/zong)+1;
		if (id==1){
			$("#pageNum").val(1);
		}else if (id==2){
			$("#pageNum").val(dang-1);
		}else if (id==3){
			$("#pageNum").val(dang+1);
		}else if (id==4){
			$("#pageNum").val(mo);
		}
		select()
	}

	function save() {
		var typeTemplateVO = {};
		var dataList = $("#addBrandList").select2('data')
		var brandList = []
		$(dataList).each(function(i,e) {
		    var brand = {id:e.id,text:e.text}
		    brandList.push(brand)
		})

		var dataList1 = $("#addSpecList").select2('data')
		var specList = []
		$(dataList1).each(function(i,e) {
		    var brand = {id:e.id,text:e.text}
			specList.push(brand)
		})
		//扩展属性
		var optionList = [];
		var trList = $("#tbody1").find("tr")
		console.log(trList)
		$(trList).each(function (i, e) {
			var tdList = $(e).find("td");
			var option = {"text": $(tdList[0]).find("input").val()}
			optionList.push(option);
		})
		var name = $("#name").val();
		typeTemplateVO['name'] = name
		typeTemplateVO['brandIds']=JSON.stringify(brandList)
		typeTemplateVO['specIds']=JSON.stringify(specList)
		typeTemplateVO["customAttributeItems"] = JSON.stringify(optionList);
		$.ajax({
			headers : {"AutharizeToken":token},
			url: domain+"/TypeTemplateController/addType",
			contentType: 'application/json;charset=utf-8',
			type:"post",
			dataType:"json",
			//将传到后台的数据转为json格式
			data: JSON.stringify(typeTemplateVO),
			//异步请求
			async:false,
			success:function (result) {
				if (result.code==10000){
					alert("增加成功")
					location.reload()
				}
			},error:function (){
				alert("系统异常")
			}
		})
	}

	function getTemplateById(id) {
		$.ajax({
			headers : {"AutharizeToken":token},
			url:domain+"/TypeTemplateController/selectById",
			type : "POST",
			data:{id:id},
			dataType : "json",
			/*contentType : "application/json;charset=utf-8",*/
			async : false,
			success : function(result) {
				if (result.code == 10000) {
				    var typeTemplateVO = result.data;
				    var abrandList = JSON.parse(typeTemplateVO.brandIds);
				    var ids = []
				    $(abrandList).each(function (i,e) {
						ids.push(e.id)
					})
					$("#updateBrandList").val(ids).trigger("change")

					var specList = JSON.parse(typeTemplateVO.specIds);
				    var ids = []
				    $(specList).each(function (i,e) {
						ids.push(e.id)
					})
					$("#updateSpecList").val(ids).trigger("change")

					//回显扩展属性
					var customAttributeItemsList = JSON.parse(typeTemplateVO.customAttributeItems);
					var cust = "";
					$(customAttributeItemsList).each(function (i, e) {
						cust += "<tr><td><input class='form-control' placeholder='属性名称' value='" + e.text + "'></td><td>"
						cust += "<button type='button' class='btnDelete' title='删除'><i class='fa fa-trash-o'></i> 删除</button></td></tr>"
					})
					$("#tbody2").html(cust);
					var name = typeTemplateVO.name;
					$("#updateName").val(name)
					var id = typeTemplateVO.id;
					$("#idd").val(id)
				}
			},error : function() {
				alert("系统错误")
			}
		})
	}

	//修改
	function updateTemplate() {
		var template = {};

		var brandSelectUpdate = $("#updateBrandList").select2("data");
		var specSelectUpdate = $("#updateSpecList").select2("data");
		var brandList = [];
		var specList = [];
		$(brandSelectUpdate).each(function (i, v) {
			var brand = {id: v.id, text: v.text};
			brandList.push(brand);
		})
		$(specSelectUpdate).each(function (i, v) {
			var spec = {id: v.id, text: v.text};
			specList.push(spec);
		})
		//拼接扩展属性
		var optionList = [];
		var trList = $("#tbody2").find("tr")
		$(trList).each(function (i, v) {
			var tdList = $(v).find("td");
			var option = {"text": $(tdList[0]).find("input").val()}
			optionList.push(option);
		})
		template["id"] = $("#idd").val();
		template["name"] = $("#updateName").val();
		template["specIds"] = JSON.stringify(specList);
		template["brandIds"] = JSON.stringify(brandList);
		template["customAttributeItems"] = JSON.stringify(optionList);
		$.ajax({
			headers : {"AutharizeToken":token},
			url: domain + "/TypeTemplateController/updateType",
			contentType: "application/json;charset=utf-8",//传过去的格式为json格式
			type: "post",
			dataType: "json",
			data: JSON.stringify(template),
			success: function (result) {
				if (result.code==10000) {
					alert("修改成功");
					location.reload();
				}
			},
			error: function (result) {
				alert("网络错误!");
			}
		})
	}


	//表格尾部增加一行
	$(function () {
		$("#insertTr").click(function () {
			$("#table tbody").append('<tr><td><input  class="form-control" placeholder="属性名称"></td><td><button type="button" class="btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button></td></tr>');
		});
	})

	//表格删除任意一行
	$(function () {
		$("#table").on('click', '.btn-default', function () {
			$(this).closest('tr').remove();
		});
	})

	//表格尾部增加一行
	$(function () {
		$("#insertTr1").click(function () {
			$("#table1 tbody").append('<tr><td><input  class="form-control" placeholder="属性名称"></td><td><button type="button" class="btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button></td></tr>');
		});
	})

	//表格删除任意一行
	$(function () {
		$("#table1").on('click', '.btn-default', function () {
			$(this).closest('tr').remove();
		});
	})

	function del() {
		var ids = [];
		var checkbox=$("input[name=check]");
		$(checkbox).each(function (i,v) {
			if ($(v).prop("checked")){
				ids.push($(v).val())
			}
		})
		if (ids.length <= 0) {
			alert("请选择要删除的id");
			return
		}
		console.log(ids)
		if (confirm("是否确定要删除")) {
			$.ajax({
				headers : {"AutharizeToken":token},
				url: domain + "/TypeTemplateController/delete?ids=" + ids,
				contentType: "application/json; charset=utf-8",
				dataType: "json",
				type: "delete",
				async: false,
				success: function (result) {
					if (result.code==10000) {
						alert("删除成功");
						location.reload();
					}
				}, error: function () {
					alert("系统错误")
				}
			})
		}
	}
</script>
    
</body>

</html>